define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'evaluation_data/index' + location.search,
                    add_url: 'evaluation_data/add',
                    edit_url: 'evaluation_data/edit?evaluation_id='+Config.evaluation_id,
                    del_url: 'evaluation_data/del',
                    multi_url: 'evaluation_data/multi',
                    import_url: 'evaluation_data/import',
                    table: 'evaluation_data',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'evaluation.name', title: __('Evaluation.name'), operate: 'LIKE'},
                        {field: 'evaluation.type_name', title: __('Type'), operate: false},
                        {
                            field: 'age', title: __('Age'), formatter: function (value, row, index) {
                                let age = "<div style='text-align:center;'>" + row.age;
                                if (row.age_unit == 1) {
                                    age += '个月';
                                } else {
                                    age += '年';
                                }
                                age += "</div>";
                                return age;
                            }, operate: false
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    title: __('编辑'),
                                    text: __('编辑'),
                                    extend: 'data-toggle="tooltip"',
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    url: function (row) {
                                        return '/ssphp.php/evaluation_data/edit?ids='+row.id+'&evaluation_id=' + Config.evaluation_id;
                                    },
                                    success: function (data, ret) {
                                        $('a.btn-refresh').trigger("click")
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    if (Config.evaluation_id != null) {
                        filter.evaluation_id = Config.evaluation_id;     // 设置值，type为字段名
                        op.evaluation_id = "=";      // 设置条件
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $("#addType").click(function () {
                    layer.open({
                        type: 2,
                        closeBtn: 1,
                        title: "类型设置",
                        area: ['80%', '80%'],
                        content: '/ssphp.php/type?type=evaluation',
                        success: function (layero, index) {
                        }
                    });
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
